home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / bin / augmenter < prev    next >
Text File  |  1996-11-11  |  4KB  |  165 lines

  1. #!/bin/perl
  2. # augment a base file with general values and components of augment directories
  3. $ENV{'PATH'} = "/usr/bsd:/bin/:/usr/bin:/usr/sbin:/usr/bin/X11";
  4.  
  5. #
  6. # get and set necessary locations and variables
  7. #
  8.  
  9. $userHome   = $ENV{"HOME"} ;
  10. if ($userHome eq "/") {
  11.     $userHome = "";
  12. }
  13. $DT_utilities = $ENV{"DT_utilities"} ;
  14. chdir($DT_utilities) || die "unable to cd to $DT_utilities\n";
  15. $DT_xconfirm = "$DT_utilities/DT_xconfirm";
  16. $success = 1;
  17. $failure = 0;
  18. $f = "$DT_utilities/.DT_Version";
  19. (-f $f ? `cat $f` : 0 ) =~ /(\d+)/;
  20. $version = $1;
  21. $DT_head_pattern = "#-- DT_utilities Compliant File ";
  22. $DT_end_marker = "#-- End of all DT_utilities stuff ----";
  23.  
  24. ($filename, $dflt_filename, $file_type, $file) = @ARGV;
  25. exit($failure) unless $file;
  26.  
  27.  
  28. sub path_report {
  29.     local($msg, $path) = @_;
  30.     local($sh_path);
  31.     $sh_path = $path =~ /^$userHome\/(.*)/ ? "\$HOME/$1" : $path;
  32.     print STDERR "$msg ($sh_path)\n";
  33. }
  34.  
  35. #
  36. # routines to read/delete/append/write mimetype and mailcap files
  37. #
  38.  
  39. $same    = 0;
  40. $add     = 1;
  41. $delete  = 2;
  42. $changed = 3;
  43.  
  44. sub read_file {
  45.     local($filename, $dflt_filename, $file_type) = @_;
  46.     $xfilename = $filename;
  47.     $xfile_type = $file_type;
  48.     $file_change = $same;
  49.     $do_ask = 1;
  50.     if (!open(IN, "$filename")) {
  51.         open(IN, "$dflt_filename") || 
  52.             die "Unable to open $dflt_filename\n";
  53.         $file_change = $file_change | $add;
  54.         $do_ask = 0;
  55.         &path_report("Creating $file_type file", $filename);
  56.     }
  57.     @file = <IN>;
  58.     close(IN);
  59. }
  60.  
  61. sub delete_stuff {
  62.     local($front, $back) = @_;
  63.     local($i, $j, $num_del);
  64.     for ($i=0; $i <= $#file; ++$i) {last if $file[$i] =~ /^$front/;}
  65.     return ($failure) unless $i <= $#file;
  66.     while ($file[$i] =~ /^#/) {
  67.         $file_change = $file_change | $delete;
  68.         return ($success) if splice(@file, $i, 1) =~ /^$back/;
  69.     }
  70.     for ($j=$i; $j <= $#file; ++$j) {last if $file[$j] =~ /^$back/;}
  71.     return ($success) unless $j <= $#file;
  72.     splice(@file, $i, $j - $i + 1);
  73.     $file_change = $file_change | $delete;
  74.     return ($success);
  75. }
  76.  
  77. sub get_file_version {
  78.     for (@file) {
  79.         next unless /^$DT_head_pattern V. (\d+)/;
  80.         return($1);
  81.     }
  82.     return(0);
  83. }
  84.  
  85. sub append_stuff {
  86.     local($filename, $description) = @_;
  87.     return($failure) unless open(IN, "$filename");
  88.     push(@file, "#\n#-- DT_utilities - $description ----\n#\n");
  89.     $file_change = $file_change | $add;
  90.     push(@file, <IN>);
  91.     close(IN);
  92.     return($success);
  93. }
  94.  
  95. sub write_file {
  96.     local($do_backup, $act, $to, $msg, $f);
  97.     return($success) unless $file_change;
  98.     $f = "${xfilename}.bak";
  99.     if (! -f $xfilename) {
  100.         $do_backup = 0;
  101.         undef $backup;
  102.     } elsif (-f $f) {
  103.         $do_backup = 0;
  104.         $backup = "$f exists so no backup will be done.\n\n";
  105.     } else {
  106.         $do_backup = 1;
  107.         $backup  = "The original file will be also saved as:\n";
  108.         $backup .= "${f}.\n\n";
  109.     }
  110.     if ($file_change == $add) {
  111.         $act = "add";
  112.         $to     = "to";
  113.     } elsif ($file_change == $delete) {
  114.         $act = "delete";
  115.         $to     = "from";
  116.     } else {
  117.         $act = "change";
  118.         $to     = "of";
  119.     }
  120.     if ($do_ask) {
  121.         $msg  = "In order for Toolbox related tasks to work properly, ";
  122.         $msg .= "it is necessary to $act a few lines $to:\n";
  123.         $msg .= "${xfilename}.\n\n$backup";
  124.         $msg .= "Should I modify the file?\n\n";
  125.         $msg .= "Note: Without these $xfile_type entries, ";
  126.         $msg .= "certain things will likely not work as intended.";
  127.         $reply = `$DT_xconfirm yesno "$msg"`;
  128.  
  129.         chop($reply);
  130.         return($failure) if $reply eq "No";
  131.     }
  132.  
  133.     # save the original just in case
  134.     system("/usr/bin/cp  $xfilename ${xfilename}.bak") if $do_backup;
  135.  
  136.     open(OUT, ">$xfilename") || die "Unable to write $xfilename\n";
  137.     for (@file) {print OUT;}
  138.     return($success);
  139. }
  140.  
  141. &read_file($filename, $dflt_filename, $file_type);
  142. &delete_stuff("#--FCD96_TOOLBOX_Compliant_File----", 
  143.     "#--End of TOOLBOX_SPECIFIC_THINGS");
  144. &delete_stuff("#--TOOLBOX_Compliant_File----", 
  145.     "#--End of TOOLBOX_SPECIFIC_THINGS");
  146. if (&get_file_version() < $version) {
  147.     &delete_stuff($DT_head_pattern, $DT_end_marker);
  148.     push(@file, "\n$DT_head_pattern V. $version ----\n");
  149.     push(@file, 
  150.         "# Do not delete the above line or the end line.\n");
  151.     push(@file, "# DT_utlities uses them to know its stuff.\n");
  152.     &append_stuff($file, "main stuff");
  153.     opendir(DIR, ".") || die "Unable to read current directory\n";
  154.     for $dir (readdir(DIR)) {
  155.         next unless -d $dir;
  156.         next if $dir =~ /^\.{1,2}$/;
  157.         $f = "$dir/$file";
  158.         &append_stuff($f, "$dir augment stuff");
  159.     }
  160.     closedir(DIR);
  161.     push(@file, "#\n$DT_end_marker\n");
  162. }
  163. &write_file();
  164. exit($success);
  165.